package cj.web.admin.controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.web.admin.domain.Category;
import cj.web.admin.service.CategoryService;
import cj.web.constants.MemuEnum;
import cj.web.login.controllers.BaseController;

@Path("/category")
public class CategoryController extends BaseController {

	private static Logger logger = Logger.getLogger(CategoryController.class.getName());
	
	@Autowired
	private CategoryService categoryService;
	
	@Get("show")
	public String show(Model model) {
		//MemuEnum
        model.add("MemuEnum",MemuEnum.pzglfl);
		return "/views/category/categoryList.vm";
	}
	
	@Get("getTree")
	public String getTree() {
		JSONObject jo = new JSONObject();
		List<Category> categorylis = categoryService.queryAll();
		List<Map> ret = new ArrayList<Map>();
		for (Category obj : categorylis) {

			Map map = new HashMap();

			map.put("id", obj.getId());
			map.put("pId", obj.getpId());
			map.put("name", obj.getName());
			map.put("value", obj.getValue());
			map.put("orderNo", obj.getOrderNo());
			map.put("descs", obj.getDescs());
			map.put("open", true);
			ret.add(map);
		}
		jo.put("ret", ret);
		return "@"+jo.toString();
	}

	@Post("getList")
	public String queryList(Category category) {
		//当前页  
		JSONObject jo = new JSONObject();
		
		Map rep = categoryService.query(category);
		
		List<Category> objs = (List<Category>) rep.get("categorylis");
		
		int size = (int) rep.get("size");
		jo.put("rows", objs);
		jo.put("total", size);
		return "@"+jo.toString();
	}
	
	@Post("create")
	public String create(Model model,Category category,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		category.setDeleteFlag(Constants.deleteFlag.deleteFlag_1);
		category.setCreateAt(DateUtils.getNowTime());
		category.setCreateUser(returnSessionObj(request).getAccount());
		category.setUpdateAt(DateUtils.getNowTime());
		category.setUpdateUser(returnSessionObj(request).getAccount());
		
		try{
			categoryService.create(category);
		}
		catch(Exception e){
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Post("update")
	public String update(Model model,Category category,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		category.setUpdateAt(DateUtils.getNowTime());
		category.setUpdateUser(returnSessionObj(request).getAccount());
		
		try {
			categoryService.update(category);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("Ids") String Ids){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		for(String id :Ids.split(",")){
			categoryService.deleteById(Integer.parseInt(id));
		}
		
		return "@"+jo.toString();
	}
}
